package com.dubbo.product.service;

import com.dubbo.api.ProductServiceApi;
import com.dubbo.pojo.Product;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Service
public class ProductImpl implements ProductService {
   @Reference
   private ProductServiceApi productServiceApi;
    @Override
    public int addProduct(Product product) {
        return productServiceApi.addProduct(product);
    }

    @Override
    public List<Product> showList() {
        return productServiceApi.showList();
    }

    @Override
    @Cacheable(cacheNames = "com.redis",key = "'product'+ #id")
    public Product showOne(int id) {
        Product product = productServiceApi.showOne(id);
        System.out.println("--查找数据库了--");
        return product;
    }



    //获取redis的操作
    @Autowired
    private RedisTemplate redisTemplate;

    public Product showOne2(int id) {
        Object obj = redisTemplate.boundValueOps("com.redis::product" + id).get();
        if (obj != null){
            return (Product)obj;
        }else {
            Product product = productServiceApi.showOne(id);
            redisTemplate.boundValueOps("com.redis::product" + id).set(product);
            System.out.println("--查找数据库了--");
            return product;
        }
    }



    //不使用@Cacheable的情况下

    public void updateCache(int id){
        String key ="com.redis::product"+id;

        //先查询redis里有没有要查询的内特
        Object obj = redisTemplate.boundValueOps(key).get();
        //从数据库中查找
        Product product = productServiceApi.showOne(id);
        if (obj == null) {
            //没有的话,直接覆盖
           redisTemplate.boundValueOps(key).set(product);
        }else {
            //如果redis里没有，缓存到redis里
            redisTemplate.boundValueOps(key).set(product);
        }
    }

    /**
     * 逻辑：如果redis里面有com.redis::product+id,直接覆盖
     * 如果不存在就直接缓存在redis里
     * @param id
     * @return
     */
    @Override
    @CachePut(cacheNames = "com.redis",key = "'product'+#id")
    public Product updateCache2(@PathVariable int id) {
        Product product = productServiceApi.showOne(id);
        return product;
    }

    /**
     * 删除缓存
     * 手动
     * @param id
     */
    public  void deletecache(int id){
        String key = "com.redis::product"+id;
        redisTemplate.delete(key);
    }

    /**
     * 删除缓存
     * 注释
     * @param id
     * */

    @CacheEvict(cacheNames = "com.redis",key = "'product'+#id")
    public  void deletecache2(@PathVariable int id){
        redisTemplate.delete(id);
    }


    public Long singTo(String userid){
        Long i = redisTemplate.boundValueOps(userid).increment();
        return i;
    }



}
